package com.atguigu.guli.service.cms.service.impl;


import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.entity.vo.AdVo;
import com.atguigu.guli.service.cms.mapper.AdMapper;
import com.atguigu.guli.service.cms.service.AdService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 广告推荐 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-09
 */
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {

    //@Autowired
    //OssFileService ossFileService;

    @Override
    public Page<AdVo> selectPage(Page<AdVo> page) {

        QueryWrapper<Ad> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("a.type_id","a.sort");
        List<AdVo> records = baseMapper.selectPageByQueryWrapper(page, queryWrapper);
        page.setRecords(records);
        return page;
    }

    /*@Override
    public boolean removeAdImageById(String id) {
        Ad ad = baseMapper.selectById(id);
        if (ad!= null){
            String imageUrl = ad.getImageUrl();
            if (!StringUtils.isEmpty(imageUrl)){
                //删除图片
                R r = ossFileService.removeFile(imageUrl);
                return r.getSuccess();
            }
        }
        return false;
    }*/

    @Cacheable(value = "ad",key = "'ads'")
    @Override
    public Map<String,Object> listAds() {

        //LambdaQueryWrapper<Ad> queryWrapper = new LambdaQueryWrapper<>();
        //先将广告分类，再对一类的广告进行排序
        //queryWrapper.orderByAsc(Ad::getTypeId,Ad::getSort);

        List<Ad> ads = baseMapper.selectList(null);
        List<Ad> banners = new ArrayList<>();
        List<Ad> teachers = new ArrayList<>();
        List<Ad> courses = new ArrayList<>();
        for (Ad ad : ads) {
            if ("1".equals(ad.getTypeId())){
                banners.add(ad);
            }else if ("2".equals(ad.getTypeId())){
                teachers.add(ad);
            }else if ("3".equals(ad.getTypeId())){
                courses.add(ad);
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("banners",banners);
        map.put("teachers",teachers);
        map.put("courses",courses);

        return map;
    }
}
